package com.awesomeproject.module

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.tencent.mmkv.MMKV

class RNMMKVModule(reactApplicationContext: ReactApplicationContext) :
    ReactContextBaseJavaModule(reactApplicationContext) {

    private val mmkv by lazy {
        MMKV.defaultMMKV()
    }

    override fun getName(): String {
        return "RNMMKVModule"
    }

    @ReactMethod(isBlockingSynchronousMethod = true)
    fun save(key: String?, value: String?): Boolean {
        if (key.isNullOrBlank() || value.isNullOrBlank()) return false
        return mmkv.putString(key, value).commit()
    }


    @ReactMethod(isBlockingSynchronousMethod = true)
    fun get(key: String?): String? {
        if (key.isNullOrBlank()) return null
        return mmkv.getString(key, null)
    }
}